package View;


import Entity.Robot;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class SupervisorView extends JFrame {
    private JList<Robot> robotListUI;
    private DefaultListModel<Robot> robotListModel;
    private JTextArea logTextArea;
    private JTextArea messageTextArea;
    private JSpinner orderFrequencySpinner;
    private JSpinner productFrequencySpinner;
    private JButton deactivateButton;
    private JButton reactivateButton;
    private JButton applyFrequencyButton;

    public SupervisorView() {
        setTitle("Supervisor Controller");
        setSize(1200, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        initGUI();
        setLocationRelativeTo(null);
    }

    private void initGUI() {
        // 左侧面板：机器人状态和控制
        JPanel leftPanel = new JPanel(new BorderLayout());
        leftPanel.setBorder(BorderFactory.createTitledBorder("Robot Status"));

        robotListModel = new DefaultListModel<>();
        robotListUI = new JList<>(robotListModel);
        robotListUI.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane robotScrollPane = new JScrollPane(robotListUI);
        robotScrollPane.setPreferredSize(new Dimension(300, 600));
        leftPanel.add(robotScrollPane, BorderLayout.CENTER);

        JPanel buttonsPanel = new JPanel(new GridLayout(2, 1, 5, 5));
        deactivateButton = new JButton("Deactivate Robot");
        reactivateButton = new JButton("Reactivate Robot");
        buttonsPanel.add(deactivateButton);
        buttonsPanel.add(reactivateButton);
        leftPanel.add(buttonsPanel, BorderLayout.SOUTH);

        add(leftPanel, BorderLayout.WEST);

        // 中央面板：日志和消息
        JPanel centerPanel = new JPanel(new GridLayout(2, 1));

        JPanel logPanel = new JPanel(new BorderLayout());
        logPanel.setBorder(BorderFactory.createTitledBorder("Robots Activity Logs"));
        logTextArea = new JTextArea();
        logTextArea.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(logTextArea);
        logPanel.add(logScrollPane, BorderLayout.CENTER);
        centerPanel.add(logPanel);

        JPanel messagePanel = new JPanel(new BorderLayout());
        messagePanel.setBorder(BorderFactory.createTitledBorder("Messages from Supply and Customer Orders"));
        messageTextArea = new JTextArea();
        messageTextArea.setEditable(false);
        JScrollPane messageScrollPane = new JScrollPane(messageTextArea);
        messagePanel.add(messageScrollPane, BorderLayout.CENTER);
        centerPanel.add(messagePanel);

        add(centerPanel, BorderLayout.CENTER);

        // 右侧面板：模拟控制
        JPanel rightPanel = new JPanel(new GridLayout(2, 1));
        rightPanel.setBorder(BorderFactory.createTitledBorder("Simulation Controls"));

        JPanel orderPanel = new JPanel();
        orderPanel.add(new JLabel("Order Arrival Frequency (ms):"));
        orderFrequencySpinner = new JSpinner(new SpinnerNumberModel(5000, 100, 10000, 100));
        orderPanel.add(orderFrequencySpinner);
        rightPanel.add(orderPanel);

        JPanel productPanel = new JPanel();
        productPanel.add(new JLabel("Product Arrival Frequency (ms):"));
        productFrequencySpinner = new JSpinner(new SpinnerNumberModel(5000, 100, 10000, 100));
        productPanel.add(productFrequencySpinner);
        rightPanel.add(productPanel);

        add(rightPanel, BorderLayout.EAST);

        JPanel bottomPanel = new JPanel();
        applyFrequencyButton = new JButton("Apply Frequencies");
        bottomPanel.add(applyFrequencyButton);
        add(bottomPanel, BorderLayout.SOUTH);
    }

    public JList<Robot> getRobotListUI() {
        return robotListUI;
    }

    public DefaultListModel<Robot> getRobotListModel() {
        return robotListModel;
    }

    public JTextArea getLogTextArea() {
        return logTextArea;
    }

    public JTextArea getMessageTextArea() {
        return messageTextArea;
    }

    public JSpinner getOrderFrequencySpinner() {
        return orderFrequencySpinner;
    }

    public JSpinner getProductFrequencySpinner() {
        return productFrequencySpinner;
    }

    public JButton getDeactivateButton() {
        return deactivateButton;
    }

    public JButton getReactivateButton() {
        return reactivateButton;
    }

    public JButton getApplyFrequencyButton() {
        return applyFrequencyButton;
    }
}
